package com.tiger.transaction.sample3;

import javax.naming.InitialContext;

public class TransferBean implements javax.ejb.SessionBean {
	private javax.ejb.SessionContext sessionContext = null;

	public void setSessionContext(javax.ejb.SessionContext sessionContext) {
		this.sessionContext = sessionContext;
	}

	public void ejbCreate() throws javax.ejb.CreateException {
	}

	public void transfer(String name1, String name2, int money) throws javax.ejb.FinderException, IllegalAccountStateException {
		try {
			InitialContext ctx = new InitialContext();
			BankAccountHome bankAccountHome = (BankAccountHome) ctx.lookup("java:comp/env/ejb/BankAccountBean");
			BankAccount bankAccountForName1 = bankAccountHome.findByPrimaryKey(name1);
			BankAccount bankAccountForName2 = bankAccountHome.findByPrimaryKey(name2);
			bankAccountForName1.withdraw(money);
			bankAccountForName2.deposit(money);
		} catch (javax.naming.NamingException e) {
			throw new javax.ejb.EJBException(e);
		} catch (javax.ejb.FinderException e) {
			sessionContext.setRollbackOnly();
			throw e;
		} catch (IllegalAccountStateException e) {
			sessionContext.setRollbackOnly();
			throw e;
		}
	}

	public void ejbActivate() {
	}

	public void ejbPassivate() {
	}

	public void ejbRemove() {
	}
}
